package com.leo;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.TextField;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;
import javafx.util.StringConverter;

/**
 * @ClassName BindingDemo
 * @Description
 * @Author wangss
 * @date 2020.05.29 11:41
 * @Version 1.0
 */
public class Binding2Demo extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        AnchorPane anchorPane = new AnchorPane();
        anchorPane.setStyle("-fx-background-color: #e7eeea");
        Scene scene = new Scene(anchorPane);

        TextField field1 = new TextField();

        TextField field2 = new TextField();

        anchorPane.getChildren().addAll(field1, field2);
        AnchorPane.setTopAnchor(field1, 100d);
        AnchorPane.setTopAnchor(field2, 100d);
        AnchorPane.setLeftAnchor(field1, 10d);
        AnchorPane.setLeftAnchor(field2, 310d);

        primaryStage.setScene(scene);
        primaryStage.setTitle("binding");
        primaryStage.setWidth(625d);
        primaryStage.setHeight(500d);
        primaryStage.setResizable(false);
        primaryStage.show();

//        field1.textProperty().bindBidirectional(field2.textProperty());

        // 这里可用用于数字和汉字的对应转换的转换
        field1.textProperty().bindBidirectional(field2.textProperty(), new StringConverter<String>() {
            @Override
            public String toString(String object) {
                return object;
            }

            @Override
            public String fromString(String string) {
                if (string.contains("1")) {
                    string = string.replace("1", "壹");
                }

                if (string.contains("2")) {
                    string = string.replace("2", "贰");
                }
                return string;
            }
        });
    }
}

